import LRU from 'lru-cache';
import isNil from 'lodash/isNil';
import { EventEmitter } from 'events';
const evt = new EventEmitter();
const cache = new LRU({
  max: 500
});
export default {
  init(key) {
    cache.set(key, {
      status: 'pending'
    });
  },
  get(key) {
    return cache.get(key).data;
  },
  has(key) {
    const item = cache.get(key);
    return !!item && !isNil(item.data);
  },
  set(key, value, options) {
    cache.set(key, {
      data: value,
      status: 'fullfilled'
    }, options.maxAge);
    evt.emit(key, value);
  },
  on(key, callback) {
    evt.on(key, callback);
  },
  delete(key) {
    cache.del(key);
  },
  getStatus(key) {
    return cache.has(key) ? cache.get(key).status : undefined;
  }
};
